package com.crazy.rabbitmq.mq;

import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {
	
	
	@Bean(name = "globalMQ")
    @ConfigurationProperties(prefix = "custom.rabbitmq.global")
    public RabbitMQ authenticateMQ() {
        return new RabbitMQ();
    }
	
	@Bean(name = "globalConnectionFactory")
	public CachingConnectionFactory buildConnectionFactory(@Qualifier("globalMQ") RabbitMQ rabbit) {
		CachingConnectionFactory factory = new CachingConnectionFactory();
		factory.setAddresses(rabbit.getAddresses());
		factory.setUsername(rabbit.getUsername());
		factory.setPassword(rabbit.getPassword());
		factory.setVirtualHost(rabbit.getVirtualHost());
		factory.setConnectionTimeout(rabbit.getConnectionTimeout());
		return factory;
	}
}
